package il.ac.tau.cvws.scrabblesidekick.server.util.math.shape;

import il.ac.tau.cvws.scrabblesidekick.shared.util.math.Point;
import il.ac.tau.cvws.scrabblesidekick.shared.util.math.Vector;

public class Rectangle extends Shape
{
	private Point p0;
	private Point p1;
	private Point p2;
	
	/*
	 *    p0------p1
	 *    |       |
	 *    |       |
	 *    p2------p3 = p1 + (p2 - p0)
	 */
	public Rectangle(Point p0, Point p1, Point p2)
	{
		this.p0 = p0;
		this.p1 = p1;
		this.p2 = p2;
	}

	public Point[][] createGrid(int dimension)
	{
		Point[][] grid = new Point[dimension][dimension];
		
		Vector v1 = new Vector(p0, p1).scale(1.0 / dimension);
		Vector v2 = new Vector(p0, p2).scale(1.0 / dimension);
		
		for (int i = 0; i < dimension; i++)
		{
			for (int j = 0; j < dimension; j++)
			{
				grid[i][j] = p0.add(v1.scale(0.5 + i)).add(v2.scale(0.5 + j));
			}
		}
		
		return grid;
	}
	
	public void setP0(Point p0)
	{
		this.p0 = p0;
	}
	
	public Point getP0()
	{
		return p0;
	}
	
	public void setP1(Point p1)
	{
		this.p1 = p1;
	}
	
	public Point getP1()
	{
		return p1;
	}
	
	public void setP2(Point p2)
	{
		this.p2 = p2;
	}
	
	public Point getP2()
	{
		return p2;
	}

	public Vector getNormal(Point p)
	{
		return new Vector(p0, p1).cross(new Vector(p0, p2)).normalize();
	}
	
}
